
package hbk;

import javax.swing.JOptionPane;

/**
 *
 * @author Casper
 */
public class OpretVagt extends javax.swing.JPanel {

    private DbConnector dbCon;


    /** Creates new form OpretVagt */
    public OpretVagt() {
        initComponents();
        populateComboBoxes();
        dbCon = new DbConnector();
        vagtOprettelse();
    }


    public void populateComboBoxes() {

        // Dato
        // Talene 1-31 bliver sat ind i combo boxen via en for-løkke
        for (int i = 1; i < 32; i++) {
            stDayBox.addItem(new String(i + ""));
        }

        // Månednavnene bliver sat ind i combo boxen
        stMonthBox.addItem(new String("Januar"));
        stMonthBox.addItem(new String("Februar"));
        stMonthBox.addItem(new String("Marts"));
        stMonthBox.addItem(new String("April"));
        stMonthBox.addItem(new String("Maj"));
        stMonthBox.addItem(new String("Juni"));
        stMonthBox.addItem(new String("Juli"));
        stMonthBox.addItem(new String("August"));
        stMonthBox.addItem(new String("September"));
        stMonthBox.addItem(new String("Oktober"));
        stMonthBox.addItem(new String("November"));
        stMonthBox.addItem(new String("December"));
        

        slMonthBox.addItem(new String("Januar"));
        slMonthBox.addItem(new String("Februar"));
        slMonthBox.addItem(new String("Marts"));
        slMonthBox.addItem(new String("April"));
        slMonthBox.addItem(new String("Maj"));
        slMonthBox.addItem(new String("Juni"));
        slMonthBox.addItem(new String("Juli"));
        slMonthBox.addItem(new String("August"));
        slMonthBox.addItem(new String("September"));
        slMonthBox.addItem(new String("Oktober"));
        slMonthBox.addItem(new String("November"));
        slMonthBox.addItem(new String("December"));


        // Dato og tids boksene bliver genereret gennem for løkker
        for (int i = 1900; i < 2010; i++) {
            stYearBox.addItem(new String(i + ""));
        }
        for (int i = 1; i < 24; i++) {
            stHourBox.addItem(new String(i + ""));
        }
        for (int i = 00; i < 75; i = i + 15) {
            stMinuteBox.addItem(new String(i + ""));
        }
        for (int i = 1900; i < 2010; i++) {
            slYearBox.addItem(new String(i + ""));
        }
        for (int i = 01; i < 32; i = i++) {
            stDayBox.addItem(new String(i + ""));
        }
        for (int i = 01; i < 32; i = i++) {
            slDayBox.addItem(new String(i + ""));
        }
        for (int i = 0; i < 24; i++) {
            slHourBox.addItem(new String(i + ""));
        }
        for (int i = 00; i < 75; i = i + 15) {
            slMinuteBox.addItem(new String(i + ""));
        }
        //arbejdsområderne tilføjes
        areaBox.addItem(new String("Port 1"));
        areaBox.addItem(new String("Port 2"));
        areaBox.addItem(new String("Kiosk 1"));
        areaBox.addItem(new String("Kiosk 2"));
        areaBox.addItem(new String("Tribune"));
        areaBox.addItem(new String("Bane"));

    }

    public String getStartDate() {
        String date = "";
        int day = stDayBox.getSelectedIndex() + 1;
        int month = stMonthBox.getSelectedIndex() + 1;
        int year = stYearBox.getSelectedIndex() + 1900;

        date += year;

        if (month < 10) {
            date += "-0" + month;
        } else {
            date += "-" + month;
        }

        if (day < 10) {
            date += "-0" + day;
        } else {
            date += "-" + day;
        }

        return date;
    }

    public String getSlutDate() {
        String date = "";
        int day = slDayBox.getSelectedIndex() + 1;
        int month = slMonthBox.getSelectedIndex() + 1;
        int year = slYearBox.getSelectedIndex() + 1900;

        date += year;

        if (month < 10) {
            date += "-0" + month;
        } else {
            date += "-" + month;
        }

        if (day < 10) {
            date += "-0" + day;
        } else {
            date += "-" + day;
        }

        return date;
    }

    public void vagtOprettelse() {
        //henter fra boxene
        int startYear = Integer.parseInt(stYearBox.getSelectedItem().toString());
        int slutYear = Integer.parseInt(slYearBox.getSelectedItem().toString());
        int startMonth = stMonthBox.getSelectedIndex() + 1;
        int slutMonth = slMonthBox.getSelectedIndex() + 1;
        int startDate = stDayBox.getSelectedIndex() + 1;
        int slutDate = slDayBox.getSelectedIndex() + 1;
        int startHour = stHourBox.getSelectedIndex() + 1;
        int slutHour = slHourBox.getSelectedIndex() + 1;
        int startMinute = Integer.parseInt(stMinuteBox.getSelectedItem().toString());
        int slutMinute = Integer.parseInt(slMinuteBox.getSelectedItem().toString());
        String area = areaBox.getSelectedItem().toString();
        //definere start og sluttid i format til databasen
        String startTid = "startHour"+":"+"startMinute";
        String slutTid = "slutHour"+":"+"slutMinute";
        



        if (startYear <= slutYear) {

            if (startMonth <= slutMonth) {

                if (startDate <= slutDate) {

                    if (startHour <= slutHour) {

                        if (startMinute < slutMinute) {

                            dbCon.executeUpdate("INSERT INTO vagter (startdato, slutdato, starttid, sluttid) VALUES ("+getStartDate()+","+getSlutDate()+","+startTid+","+slutTid+")");

                        } else {
                            JOptionPane.showMessageDialog(null, "Slut dato skal være efter start dato!");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "Slut dato skal være efter start dato!");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Slut dato skal være efter start dato!");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Slut dato skal være efter start dato!");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Slut dato skal være efter start dato!");
        }
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        stDayBox = new javax.swing.JComboBox();
        stYearBox = new javax.swing.JComboBox();
        stMonthBox = new javax.swing.JComboBox();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        stHourBox = new javax.swing.JComboBox();
        jTextField3 = new javax.swing.JTextField();
        slDayBox = new javax.swing.JComboBox();
        slMonthBox = new javax.swing.JComboBox();
        slYearBox = new javax.swing.JComboBox();
        jTextField4 = new javax.swing.JTextField();
        slHourBox = new javax.swing.JComboBox();
        stMinuteBox = new javax.swing.JComboBox();
        slMinuteBox = new javax.swing.JComboBox();
        areaBox = new javax.swing.JComboBox();
        jTextField5 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jTextField1.setBackground(new java.awt.Color(240, 240, 240));
        jTextField1.setEditable(false);
        jTextField1.setText("Start Dato");
        jTextField1.setBorder(null);

        jTextField2.setBackground(new java.awt.Color(240, 240, 240));
        jTextField2.setEditable(false);
        jTextField2.setText("Start Tid");
        jTextField2.setBorder(null);

        jTextField3.setEditable(false);
        jTextField3.setText("Slut Dato");
        jTextField3.setBorder(null);

        jTextField4.setBackground(new java.awt.Color(240, 240, 240));
        jTextField4.setEditable(false);
        jTextField4.setText("Slut Tid");
        jTextField4.setBorder(null);

        jTextField5.setBackground(new java.awt.Color(240, 240, 240));
        jTextField5.setEditable(false);
        jTextField5.setText("Område");
        jTextField5.setBorder(null);

        jButton1.setText("Opret");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(stDayBox, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(stMonthBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(stHourBox, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.Alignment.LEADING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(stMinuteBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(slHourBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                                    .addComponent(slDayBox, javax.swing.GroupLayout.Alignment.LEADING, 0, 45, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(slMonthBox, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(slMinuteBox, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(slYearBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(stYearBox, 0, 68, Short.MAX_VALUE))))
                .addContainerGap(70, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(210, 210, 210))
            .addGroup(layout.createSequentialGroup()
                .addComponent(areaBox, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stDayBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(stMonthBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(stYearBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stHourBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(stMinuteBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slDayBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(slMonthBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(slYearBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slHourBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(slMinuteBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(areaBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(17, 17, 17))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
     vagtOprettelse();
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox areaBox;
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JComboBox slDayBox;
    private javax.swing.JComboBox slHourBox;
    private javax.swing.JComboBox slMinuteBox;
    private javax.swing.JComboBox slMonthBox;
    private javax.swing.JComboBox slYearBox;
    private javax.swing.JComboBox stDayBox;
    private javax.swing.JComboBox stHourBox;
    private javax.swing.JComboBox stMinuteBox;
    private javax.swing.JComboBox stMonthBox;
    private javax.swing.JComboBox stYearBox;
    // End of variables declaration//GEN-END:variables
}
